// var firstUniqChar = function (s) {
//   const position = new Map()
//   const n = s.length
//   for (let [i, ch] of Array.from(s).entries()) {
//     if (position.has(ch)) {
//       position.set(ch, -1)
//     } else {
//       position.set(ch, i)
//     }
//   }
//   let first = n
//   for (let pos of position.values()) {
//     if (pos !== -1 && pos < first) {
//       first = pos
//     }
//   }
//   return first == n ? ' ' : s[first]
// }

var firstUniqChar = function (s) {
  const hash = new Map()
  const queue = []
  for (let [k, v] of Array.from(s).entries()) {
    if (!hash.has(v)) {
      hash.set(v, k)
      queue.push([v, k])
    } else {
      while (queue.length && queue[0][0] === v) {
        queue.shift()
      }
    }
  }
  return queue.length ? queue[0] : ''
}

console.log(firstUniqChar('aabbcde'))
